package edu.upb.acs.jobsmaps.activity.details;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import edu.upb.acs.jobsmaps.R;
import edu.upb.acs.jobsmaps.database.task.AddRatingTask;

/**
 * Created by dani on 6/29/2014.
 */
public class RatingActivity extends Activity {

	private String TAG =  "RatingActivity";

	RatingBar ratingBar;

	TextView ratingText;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rating);

		ratingBar = (RatingBar) findViewById(R.id.ratingBar1);

		Button goBackButton = (Button) findViewById(R.id.cancelRate);
		goBackButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				setResult(RESULT_OK);
				finish();
			}
		});

		Bundle extras = getIntent().getExtras();
		Integer id= null;
		if (extras != null) {
			id = extras.getInt("id");
			Log.d(TAG, "Add rate for company with id = " + id);
		}else {
			Log.e(TAG, "No job id received");
			throw new RuntimeException("Called RatingActivity without job id");
		}

		Button okButton = (Button) findViewById(R.id.rate);
		final Integer finalId = id;
		okButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				AddRatingTask addRatingTask = new AddRatingTask();
				try {
					float rating = ratingBar.getRating();
					Log.d(TAG, "*** rating = " + rating);

					Integer persistedRatingId = addRatingTask.execute(String.valueOf(rating), String.valueOf(finalId)).get();
					Log.d(TAG, "New rating id = " + persistedRatingId);
				} catch (Exception e) {
					Log.e(TAG, "Can not persist rating", e);
				}

				Intent i = new Intent(RatingActivity.this,
						ViewCompanyDetailsActivity.class);
				i.putExtra("rating", "yes");
				i.putExtra("company_id", finalId.toString());
				startActivity(i);
			}
		});

	}


}
